package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.duer.libcore.util.ConsoleLogger;
import com.baidu.duer.smartmate.protocol.dlp.bean.audioplayer.AudioplayerStatusPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.audioplayer.PlaybackInfoPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.ButtonClicked;
import com.baidu.duer.smartmate.proxy.bean.PlaybackCommand;
import com.baidu.duer.smartmate.proxy.bean.RenderPlayListMessage;
import com.baidu.duer.smartmate.proxy.bean.RenderPlayerMessage;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;
import com.baidu.duer.smartmate.proxy.controller.DCSRenderPlayerObserver;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.util.DemoUtils;

/**
 * Created by niufei01 on 2017/9/11.
 * <p>
 * 播放器Demo页
 */

public class AudioPlayerDemo extends BaseFragment implements DCSRenderPlayerObserver {

    ControllerManager controllerManager;
    ViewHolder viewHolder = null;

    String mToken;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.audio_player, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        viewHolder = new ViewHolder(view);
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerAudioPlayerObserver(this);
        controllerManager.registerPlayListObserver(playerListObserver);
        viewHolder.getstatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.getAudioPlayerStatus();
                controllerManager.getRenderPlayerInfoStatus();
            }
        });
        viewHolder.previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToken == null) {
                    ConsoleLogger.printDebugInfo(AudioPlayerDemo.class, "current player is empty");
                    return;
                }
                controllerManager.btnClickedCommand(ButtonClicked.PREVIOUS, mToken);
            }
        });
        viewHolder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToken == null) {
                    ConsoleLogger.printDebugInfo(AudioPlayerDemo.class, "current player is empty");
                    return;
                }
                controllerManager.btnClickedCommand(ButtonClicked.PLAY_PAUSE, mToken);
            }
        });
        viewHolder.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToken == null) {
                    ConsoleLogger.printDebugInfo(AudioPlayerDemo.class, "current player is empty");
                    return;
                }
                controllerManager.btnClickedCommand(ButtonClicked.PLAY_PAUSE, mToken);

            }
        });

        viewHolder.next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToken == null) {
                    ConsoleLogger.printDebugInfo(AudioPlayerDemo.class, "current player is empty");
                    return;
                }
                controllerManager.btnClickedCommand(ButtonClicked.NEXT, mToken);

            }
        });
        viewHolder.showList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToken == null) {
                    ConsoleLogger.printDebugInfo(AudioPlayerDemo.class, "current player is empty");
                    return;
                }
                controllerManager.btnClickedCommand(ButtonClicked.SHOW_PLAYLIST, mToken);
            }
        });
        viewHolder.playNoScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                controllerManager.playbackControllerCommand(PlaybackCommand.PLAY);
            }
        });
        viewHolder.stopNoScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                controllerManager.playbackControllerCommand(PlaybackCommand.STOP);
            }
        });
        viewHolder.previousNoScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                controllerManager.playbackControllerCommand(PlaybackCommand.PREVIOUS);
            }
        });
        viewHolder.nextNoScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                controllerManager.playbackControllerCommand(PlaybackCommand.NEXT);
            }
        });
    }

    @Override
    public void onRenderPlayInfoDataChanged(String event, RenderPlayerMessage message) {

        this.mToken = message.getToken();
        viewHolder.onRenderPlayInfoDataChanged.setText(message != null ? DemoUtils.toFormatJson(message) : "数据为空");
    }

    @Override
    public void onPlaybackInfoDataChanged(String event, PlaybackInfoPayload playbackInfoPayload) {
        viewHolder.onPlaybackChanaged.setText(event);
    }

    @Override
    public void onDataChanaged(String event, AudioplayerStatusPayload message) {
        this.mToken = message.getToken();
        viewHolder.onDataChanaged.setText(message != null ? DemoUtils.toFormatJson(message) : "数据为空");

    }

    DCSDataObserver<RenderPlayListMessage> playerListObserver = new DCSDataObserver<RenderPlayListMessage>() {
        @Override
        public void onDataChanaged(String event, RenderPlayListMessage message) {
            viewHolder.onRenderPlayListDataChanged.setText(message != null ? DemoUtils.toFormatJson(message) : "数据为空");
        }
    };


    public class ViewHolder {
        public EditText onDataChanaged;
        public EditText onPlaybackChanaged;
        public EditText onRenderPlayInfoDataChanged;
        public EditText onRenderPlayListDataChanged;
        public Button getstatus;
        public Button previous;
        public Button play;
        public Button stop;
        public Button next;
        public Button showList;
        public Button previousNoScreen;
        public Button playNoScreen;
        public Button stopNoScreen;
        public Button nextNoScreen;

        public ViewHolder(View rootView) {
            this.onDataChanaged = (EditText) rootView.findViewById(R.id.onDataChanaged);
            this.onPlaybackChanaged = (EditText) rootView.findViewById(R.id.onPlaybackChanaged);
            this.onRenderPlayInfoDataChanged = (EditText) rootView.findViewById(R.id.onRenderPlayInfoDataChanged);
            this.onRenderPlayListDataChanged = (EditText) rootView.findViewById(R.id.onRenderPlayListDataChanged);

            this.getstatus = (Button) rootView.findViewById(R.id.getstatus);
            this.previous = (Button) rootView.findViewById(R.id.previous);
            this.showList = (Button) rootView.findViewById(R.id.showList);
            this.play = (Button) rootView.findViewById(R.id.play);
            this.stop = (Button) rootView.findViewById(R.id.stop);
            this.next = (Button) rootView.findViewById(R.id.next);
            this.previousNoScreen = (Button) rootView.findViewById(R.id.previous_no_screen);
            this.playNoScreen = (Button) rootView.findViewById(R.id.play_no_screen);
            this.stopNoScreen = (Button) rootView.findViewById(R.id.stop_no_screen);
            this.nextNoScreen = (Button) rootView.findViewById(R.id.next_no_screen);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (controllerManager != null) {
            controllerManager.unregisterAudioPlayerObserver(this);
            controllerManager.unregisterPlayListObserver(playerListObserver);
        }
    }
}
